/*
 * @Author: aggfb 714740235@qq.com
 * @Date: 2024-05-18 14:29:53
 * @LastEditors: aggfb 714740235@qq.com
 * @LastEditTime: 2024-05-18 21:57:30
 * @FilePath: \2.code\label_rack_control\firmware\model\param\param.c
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

#include "param.h"

#define FLASH_START_PARAM  BANK1_WRITE_START_ADDR                            /* flash开始存储参数 */
#define FLASH_START_PARAM_OFFSET_SLEF    FLASH_START_PARAM                   /* flash偏移位置(防止在一个位置频繁擦写) */
#define FLASH_START_PARAM_OFFSET        (FLASH_START_PARAM_OFFSET_SLEF + 16) /* offset值 */
#define FLASH_START_PARAM_SPEED_RATIO   (FLASH_START_PARAM_OFFSET + 16)      /* 速度值 */
#define FLASH_START_PARAM_RUN_MODE      (FLASH_START_PARAM_SPEED_RATIO + 16) /* 手动自动模式 */

/**
 * 保存参数
*/
void param_save(void)
{
    motor_ctrl_t* motor_ctrl = motor_ctrl_dev();
    bsp_flash_erase_page(FLASH_START_PARAM);
    //bsp_flash_write(FLASH_START_PARAM_OFFSET_SLEF,    );
    bsp_flash_write(FLASH_START_PARAM_OFFSET,           motor_ctrl->offset_cmd);
    bsp_flash_write(FLASH_START_PARAM_SPEED_RATIO,      motor_ctrl->speed_ratio_cmd);
    //bsp_flash_write(FLASH_START_PARAM_RUN_MODE,         motor_ctrl->manual_automatic);
}

/**
 * 获取参数
*/
void param_gain(void)
{
    motor_ctrl_t* motor_ctrl = motor_ctrl_dev();
    motor_ctrl->offset_cmd = bsp_flash_read(FLASH_START_PARAM_OFFSET);
    motor_ctrl->speed_ratio_cmd = bsp_flash_read(FLASH_START_PARAM_SPEED_RATIO);

    /* 读出数据有误,置为默认值 */
    if(motor_ctrl->offset_cmd > MOTOR_OFFSET_MAX)
    {
        motor_ctrl->offset_cmd = MOTOR_OFFSET_MIN;
    }
    if((motor_ctrl->speed_ratio_cmd > MOTOR_SPEED_MIN) || (motor_ctrl->speed_ratio_cmd < MOTOR_SPEED_MAX))
    {
        motor_ctrl->speed_ratio_cmd = MOTOR_SPEED_MIN;
    }
}
